package com.example.myapplication9;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class search extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        ListView lstv=findViewById(R.id.show);
        SimpleAdapter adapter=new SimpleAdapter(search.this,mylst(),R.layout.lstitem,new String[]{"id","title","A","B","C","D"},new int[]{R.id.num,R.id.contain,R.id.choA,R.id.choB,R.id.choC,R.id.choD});
        lstv.setAdapter(adapter);
    }
    public ArrayList<Map<String,Object>> mylst()
    {
        MyDBHelper myDBHelper=new MyDBHelper(search.this,"learn");
        SQLiteDatabase db=myDBHelper.getWritableDatabase();
        ArrayList<Map<String,Object>> lst=new ArrayList<Map<String,Object>>();
        String[] key=new String[]{"id","title","A","B","C","D"};
        @SuppressLint("Recycle") Cursor cursor= db.query("question",new String[]{"id","title","A","B","C","D"},null,null,null,null,null);
        int n=cursor.getCount();
        cursor.moveToFirst();
        for(int i=0;i<n;i++)
        {
            HashMap<String,Object> item=new HashMap<String,Object>();
            for(int j=0;j<6;j++)
            {
                int index=cursor.getColumnIndex(key[j]);
                item.put(key[j],cursor.getString(index));
            }
            lst.add(item);
            cursor.moveToNext();
        }
        db.close();
        return lst;
    }
    @SuppressLint("Range")
    public void searchid(View view) {
        EditText id=findViewById(R.id.id);
        MyDBHelper myDBHelper=new MyDBHelper(search.this,"learn");
        SQLiteDatabase db=myDBHelper.getWritableDatabase();
        @SuppressLint("Recycle") Cursor cursor= db.query("question",new String[]{"id","title","A","B","C","D"},"id=?",new String[]{id.getText().toString()},null,null,null);
        int n=cursor.getCount();
        System.out.println(n);
        if(n==0)
        {
            Toast.makeText(this, "未查询到结果", Toast.LENGTH_SHORT).show();
        }
        else{
            cursor.moveToFirst();
            Intent intent=new Intent(search.this,show.class);
            intent.putExtra("id",cursor.getString(cursor.getColumnIndex("id")));
            intent.putExtra("title",cursor.getString(cursor.getColumnIndex("title")));
            intent.putExtra("A",cursor.getString(cursor.getColumnIndex("A")));
            intent.putExtra("B",cursor.getString(cursor.getColumnIndex("B")));
            intent.putExtra("C",cursor.getString(cursor.getColumnIndex("C")));
            intent.putExtra("D",cursor.getString(cursor.getColumnIndex("D")));
            startActivity(intent);
        }
    }

    public void back(View view) {
        Intent intent=new Intent(search.this,MainActivity.class);
        startActivity(intent);
    }
}